<?php
/**
 *
 * @author Strawberry🍓
 * @date 2021-05-31 3:12 下午
 *
 */

namespace app\api\controller;


use app\common\model\tang\CostType;
use app\common\model\tang\SubjectList;
use think\Model;

class Cost extends Base
{

    public function utilityBills()
    {
//         $merchant_sn = \app\common\model\tang\Merchant::where(['id'=>$this->merchant_id])->value('merchant_sn');
//         $result = [
//             'water'  => (\app\common\model\tang\System::where(['id'=>1])->value('amount'))/100,
//             'electricity' => (\app\common\model\tang\System::where(['id'=>2])->value('amount'))/100,
//             'service_charge' =>  (\app\common\model\tang\System::where(['id'=>3])->value('amount'))/100,
//             'merchant_id' => $merchant->merchant_sn
//         ];
         $merchant   =  \app\common\model\tang\Merchant::find($this->merchant_id);
         // 手续费
         $service_chargee = (new SubjectList())->where(['id'=>1])->value('rate')/100;
         //电费
         $unitprice_d = 0 ;
         $electricity = (new CostType())->where('pid','=',1)->field('cost_money')->select()->toArray();
         if(!empty($electricity)){
             foreach ($electricity as $v)
             {
                 $unitprice_d+=$v['cost_money'] ;
             }
         }
        //水费
        $unitprice_s = 0 ;
        $water = (new CostType())->where('pid','=',5)->field('cost_money,th_money')->select()->toArray();
        if(!empty($water)){
            foreach ($water as $v)
            {
                if($merchant->electricity_type == 1){
                    $unitprice_s+=$v['cost_money'] ;
                }else{
                    $unitprice_s+=$v['th_money'] ;
                }
            }
        }
        $result = [
             'water'  => round($unitprice_s,2),
             'electricity' => round($unitprice_d,2),
             'service_charge' =>  $service_chargee,
             'merchant_id' => $merchant->company
        ];

        return SendSuccess("success", $result);
    }

    public function utilityBillsPay()
    {
        $cost_type = intval(trim($this->request->param('cost_type')));// cost_type表里的ID, 如果为用户自主创建 该ID 1为电费 2为水费
        if($cost_type==2){
            $cost_type = 5 ;
        }
        $number = intval(trim($this->request->param('number')));
        if (empty($cost_type) || empty($number)) {
            return SendError("参数错误");
        }
        if ($cost_type == 1){
            $up_number = \app\common\model\tang\Merchant::where('id','=',$this->merchant_id)->value('number');
            if ($up_number < $number){
                return SendError("最大缴费数量为{$up_number}度");
            }
        }
        $merchant   =  \app\common\model\tang\Merchant::find($this->merchant_id);
//        // 手续费
//        $service_chargee = (new SubjectList())->where(['id'=>1])->value('rate')/10000;
        //电费
        $unitprice_d = 0 ;
        $electricity = (new CostType())->where('pid','=',1)->field('cost_money')->select()->toArray();
        if(!empty($electricity)){
            foreach ($electricity as $v)
            {
                $unitprice_d+=$v['cost_money'] ;
            }
        }
        //水费
        $unitprice_s = 0 ;
        $water = (new CostType())->where('pid','=',5)->field('cost_money,th_money')->select()->toArray();
        if(!empty($water)){
            foreach ($water as $v)
            {
                if($merchant->electricity_type == 1){
                    $unitprice_s+=$v['cost_money'] ;
                }else{
                    $unitprice_s+=$v['th_money'] ;
                }
            }
        }
        $result = [
            5  => $unitprice_s,
            1  => $unitprice_d,
        ];

//        $result = [
//            2  => (\app\common\model\tang\System::where(['id'=>1])->value('amount'))/100,
//            1 => (\app\common\model\tang\System::where(['id'=>2])->value('amount'))/100,
//        ];
//        $service_charge_reat = (\app\common\model\tang\System::where(['id'=>3])->value('amount'))/100;
        // 手续费
        $service_charge_reat = (new SubjectList())->where(['id'=>1])->value('rate')/100;
        $order_sn = OrderSn();
        $price = round(($number * $result[$cost_type]),2); // 计算出价格
        $service_charge = round($price * ($service_charge_reat/100),2); //手续费
        $amount   = (round($price + $service_charge,2)) * 100;
        $model = new \app\common\model\tang\Cost();
        $model->is_user = 1;
        $model->type = 2;
        $model->subject_id = 1; // 支付方式
        $model->service_charge = $service_charge * 100;
        $model->cost_type_id = $cost_type;
        $model->merchant_id = $this->merchant_id;
        $model->pay_status   = 0;
        $model->amount = $price * 100 ;
        $model->real_amount = $amount;
        $model->number = $number;
        $model->unit_price = $result[$cost_type];
        $model->order_sn = $order_sn;
        $info = $model->save();
        if (empty($info)){
            return SendError("稍后再试");
        }


        $result = $this->keli_sdk->pay->wechat_pay_miniProgram($order_sn,'1616646612','订单支付',$amount, 'https://zdwy.kelimx.com/api/electricity/notify','','',$this->openid,'wx349512c7e618972e','');
        //统一下单
//        $result = $this->app->Payment->order->unify([
//            'body' => '订单支付',
//            'out_trade_no' => $order_sn,
//            'total_fee' => $amount,
//            'notify_url' => 'https://dtxs.kelimx.cn/api/electricity/notify', // 支付结果通知网址，如果不设置则会使用配置里的默认地址
//            'trade_type' => 'JSAPI', // 请对应换成你的支付方式对应的值类型
//            'openid' => $this->openid,
//        ]);
        $result = json_decode($result, true);
        if (!empty($result['code']) && $result['code'] == '200'){
            $payper_id =  str_replace('prepay_id=','',$result['result']['package']);
            $data = $this->app->Payment->jssdk->bridgeConfig($payper_id, false);
            return SendSuccess("success", $data);
        }
        return SendError("支付失败稍后再试～", $result);
    }


}